

package ig.formato.dao;

import ig.formato.dao.abs.DAO;
import ig.formato.dto.FormulacionDTO;
import ig.formato.util.IGFormatoException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
 * 
 * @author jsanchez
 */
public class FormulacionDao extends DAO {

    public FormulacionDao() {
    }

    public Object buscar(Integer id) throws IGFormatoException {
        try {
            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, IDCAMPOPRE, NOMBRE, ACTIVA, TIPO, FORMULA1, FORMULA2, ORDEN FROM FORMULACION WHERE ID = ?");
            prep.setInt(1, id.intValue());
            resul = prep.executeQuery();
            if (resul.next()) {
                return getDatos(resul, new FormulacionDTO());
            }
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
        return null;
    }

    public Collection buscarPorCampoPre(Integer id) throws IGFormatoException {
        Collection c = new ArrayList();
        try {
            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, IDCAMPOPRE, NOMBRE, ACTIVA, TIPO, FORMULA1, FORMULA2,ORDEN FROM FORMULACION WHERE IDCAMPOPRE = ? ORDER BY ID");
            prep.setInt(1, id.intValue());
            for (resul = prep.executeQuery(); resul.next();) {
                c.add(getDatos(resul, new FormulacionDTO()));
            }
            Collections.sort((List) c);
            return c;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Collection buscarTodos() throws IGFormatoException {
        Collection c = new ArrayList();
        try {
            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, IDCAMPOPRE, NOMBRE, ACTIVA, TIPO, FORMULA1, FORMULA2,ORDEN FROM FORMULACION ORDER BY ID");
            for (resul = prep.executeQuery(); resul.next();) {
                c.add(getDatos(resul, new FormulacionDTO()));
            }
            return c;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Integer guardar(Object obj) throws IGFormatoException {
        FormulacionDTO dto = (FormulacionDTO) obj;
        try {
            getConnection();
            if (dto.getId() == null) {
                prep = connectionIgFormato.prepareStatement("INSERT INTO FORMULACION(IDCAMPOPRE, NOMBRE, ACTIVA, TIPO, FORMULA1, FORMULA2, ORDEN, ID) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
                dto.setId(getMaximoId("FORMULACION"));
            } else {
                prep = connectionIgFormato.prepareStatement("UPDATE FORMULACION SET IDCAMPOPRE=?, NOMBRE='', ACTIVA=?, TIPO=?, FORMULA1=?, FORMULA2=?, ORDEN=? WHERE ID=?");
            }
            setDatos(prep, dto, "IDCAMPOPRE, NOMBRE, ACTIVA, TIPO, FORMULA1, FORMULA2, ORDEN, ID");
            prep.executeUpdate();
            return dto.getId();
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } catch (Throwable ex) {
            throw new IGFormatoException(ex);            
        } finally {
            closeAll();
        }
    }

    public void eliminar(Integer id) throws IGFormatoException {
        try {
            getConnection();
            prep = connectionIgFormato.prepareStatement("DELETE FROM FORMULACION WHERE IDCAMPOPRE = ?");
            prep.setInt(1, id.intValue());
            prep.executeUpdate();
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Object existe(Object obj) throws IGFormatoException {
        try {
            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID FROM FORMULACION WHERE NOMBRE = ?");
            prep.setString(1, ((FormulacionDTO) obj).getNombre().trim());
            resul = prep.executeQuery();
            if (resul.next()) {
                return Integer.valueOf(resul.getInt("ID"));
            }
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
        return false;
    }
}
